package role

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/admin"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	//查询信息
	var adminGroupStruct admin.AdminGroupStruct
	var adminGroupData []admin.AdminGroupStruct
	model.GetModelData(
		model.Db,
		adminGroupStruct.TableName(),
		&adminGroupData,
		model.QueryOptions{
			Conditions: where,
			OrderBy:    "id asc",
			Debug:      true,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range adminGroupData {
		returnData = append(returnData, map[string]interface{}{
			"id":         v.Id,
			"name":       v.Name,
			"status":     v.Status,
			"created_at": v.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		adminGroupStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}
